import { routeHasExist, setTagNavListInLocalstorage } from '@/libs/util'

export default {
    state: {
        tagNavList: [],
        homeRoute: {}
    },
    mutations: {
        setHomeRoute (state, data) {
            state.homeRoute = data
        },
        setTagNavList (state, list) {
            if (list) {
                state.tagNavList = [...list]
                setTagNavListInLocalstorage([...list])
            }
        },
        addTag (state, { route, type = 'unshift' }) {
            if (!routeHasExist(state.tagNavList, route)) {
                if (type === 'push') state.tagNavList.push(route)
                else {
                    if (route.name === state.homeRoute.name) state.tagNavList.unshift(route)
                    else state.tagNavList.splice(1, 0, route)
                }
            }
        },
        /**
     * 移除某个标签导航栏
     * @param {String} route  需要移除的路由
     */
        removeTagNav (state, route) {
            const { path } = route
            const index = state.tagNavList.findIndex(item => item.path === path)
            index > -1 && state.tagNavList.splice(index, 1)
        }
    }
}
